package com.intel.factorytest.activity;


import java.io.File;
import java.io.IOException;

import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.util.Log;

import com.intel.factorytest.R;





public class SetFactoryTestActivity extends PreferenceActivity {
      
    private boolean hasSet = false;
    private final String TAG = "SetFactoryTestActivity";
    private File sampleDir;
    private File sampleDirAcat;
    private CheckBoxPreference mPreference;
    private CheckBoxPreference mPreferenceAcat;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.test_menu_set);    
        mPreference = (CheckBoxPreference) findPreference("key_toggle_skip");
        mPreferenceAcat = (CheckBoxPreference) findPreference("key_acat_log");
    //    File sampleDir = Environment.getDataDirectory();
        setSkipFun();
        setAcatFun();
        } 
    
    private void setSkipFun() {
        hasSet = hasSetStatus(1);
        Log.e(TAG,"hasSet ="+hasSet);
                mPreference.setChecked(hasSet);
        mPreference.setOnPreferenceChangeListener (new Preference.OnPreferenceChangeListener() {

        @Override
        public boolean onPreferenceChange(Preference preference, Object newValue){
            setFile((Boolean) newValue);
            return true;
            }
        });
    }
    private void setAcatFun() {
        hasSet = hasSetStatus(2);
        Log.e(TAG,"Acat hasSet ="+hasSet);
                mPreferenceAcat.setChecked(hasSet);
        mPreferenceAcat.setOnPreferenceChangeListener (new Preference.OnPreferenceChangeListener() {

        @Override
        public boolean onPreferenceChange(Preference preference, Object newValue){
            setFileAcat((Boolean) newValue);
            return true;
            }
        });
    }
    private boolean hasSetStatus (int which) {
        if (which == 1) {
            sampleDir = new File("/data/data/com.intel.factorytest/setmode.txt");
            Log.e(TAG,"sampleDir = "+sampleDir);
            if (sampleDir == null || !sampleDir.exists()) {
                return false;
            } else {
                return true;
            }
        }else {//acat
            sampleDirAcat = new File("/data/telephony/config/acat_log_enable");
            if (sampleDirAcat == null || !sampleDirAcat.exists()) {
                return false;
            } else {
                return true;
            }
        }
    } 
    private boolean setFile (boolean isSet) {
        Log.e(TAG,"setFile isSet ="+isSet+"is exists = "+sampleDir.exists());
        if (isSet) {
            try {
                    sampleDir = new File("/data/data/com.intel.factorytest/setmode.txt");
                //    sampleFile = File.createTempFile(SAMPLE_PREFIX, extension, sampleDir);
                if (!sampleDir.exists())    
                    sampleDir.createNewFile();
            } catch (IOException e) {
                Log.d(TAG,"startRecording 11");
                return false;
            }
        }else {
            if (sampleDir.exists())
            {
                boolean isDel = sampleDir.delete();
                Log.e(TAG,"isDel = "+isDel);
            }
        }
        return true;
    }

    private boolean setFileAcat (boolean isSet) {
        Log.e(TAG,"setFile isSet ="+isSet+"is exists = "+sampleDir.exists());
        if (isSet) {
            try {
                sampleDirAcat = new File("/data/telephony/config/acat_log_enable");
                //    sampleFile = File.createTempFile(SAMPLE_PREFIX, extension, sampleDir);
                if (!sampleDirAcat.exists())    
                    sampleDirAcat.createNewFile();
            } catch (IOException e) {
                Log.d(TAG,"startRecording 11");
                return false;
            }
        }else {
            if (sampleDirAcat.exists())
            {
                boolean isDel = sampleDirAcat.delete();
                Log.e(TAG,"isDel = "+isDel);
            }
        }
        return true;
    }
}
